﻿@model TestUp.Models.AdminModels.ViewPendingTestsModel
@{
    ViewBag.Title = "Manage Pending Tests";
    Layout = "~/Views/Admin/_AdminLayout.cshtml";
}

<script type="text/javascript">
    function destroy(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        var id = dataItem.GroupId;
        if (confirm("Are you sure you want to delete this pending test?")) {
            var form = document.createElement("form");
            form.setAttribute("method", "post");
            form.setAttribute("action", "DeletePendingTest");
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", "PendingTestId");
            hiddenField.setAttribute("value", id);
            form.appendChild(hiddenField);
            document.body.appendChild(form);
            form.submit();
        }
    }

    function edit(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        var id = dataItem.PendingTestId;

        var url = '@Url.Action("EditPendingTest", "Admin", new { id = "__id__" })';
        window.location.href = url.replace('__id__', id);
    }

</script>

<h2>Manage Pending Tests</h2>

@(Html.Kendo().Grid(Model.PendingTests)
      .Name("pendingTestsGrid")
      .Columns(cols =>
      {
          cols.Bound(x => x.PendingTestId).Hidden(true);
          cols.ForeignKey(x => x.GroupId, Model.Groups, "GroupId", "GroupName").Title("Group").Width("20%");
          cols.ForeignKey(x => x.TestId, Model.Tests, "TestId", "TestName").Title("Test").Width("30%");
          cols.Bound(x => x.ExpirationDate).Width("30%");
          cols.Command(cmd => cmd.Edit().Text("Edit").CancelText("Cancel").UpdateText("Apply"));
          cols.Command(cmd => cmd.Destroy().Text("Delete"));
      })
      .Editable(editable => editable.Mode(GridEditMode.PopUp).Window(window=>window.Title("Edit Item")))
      .ToolBar(cmd => cmd.Create().Text("Create New"))
      .Groupable()
      .DataSource(dataSource => dataSource
          .Server()
          .Model(model =>
          {
              model.Id(x=>x.PendingTestId);
              model.Field(x => x.GroupId).Editable(false);
              model.Field(x => x.TestId).Editable(false);
          })
          .Create(create => create.Action("CreatePendingTest", "Admin"))
          .Update(update => update.Action("EditPendingTest", "Admin"))
          .Destroy(destroy => destroy.Action("DeletePendingTest", "Admin"))
      )
      .Groupable(x=>x.Messages(builder => builder.Empty("Drop here some column header for groupping."))))
